Skip to main content

🎯 Unit 3: Decision Making and Branching

Welcome back, coder! πŸš€
This time, we’re learning how to make our C programs think and choose. Yep β€” like teaching your code how to make decisions.


πŸ€” Introduction to Decision Making​

Decision making structures let programs choose different paths depending on conditions.
Think of it like asking: β€œIf it’s raining, should I take an umbrella? If not, sunglasses!” πŸ˜Žβ˜”

C gives us:

  1. βœ… Simple if
  2. βœ… if-else
  3. βœ… Nested if
  4. βœ… else-if ladder
  5. βœ… switch-case

πŸ”Ή The if Statement​

Syntax​

if (condition) {
// Executes if condition is true
}

Flow:

Condition β†’ True β†’ Run block
β†’ False β†’ Skip block

Example​

#include <stdio.h>

int main() {
int age = 20;
if (age >= 18) {
printf("You are eligible to vote.\n");
}
printf("Thank you!\n");
return 0;
}

πŸ’‘ Tip: if doesn’t need an else. It can stand alone.


πŸ”Έ The if-else Statement​

Gives us two paths β€” one for true, one for false.

if (condition) {
// True path
} else {
// False path
}

Example: Even or Odd​

#include <stdio.h>

int main() {
int number;
printf("Enter a number: ");
scanf("%d", &number);

if (number % 2 == 0) {
printf("%d is even.\n", number);
} else {
printf("%d is odd.\n", number);
}
return 0;

}


πŸ”Ή Nested if Statements​

One if inside another.

if (condition1) {
if (condition2) {
// Both true
} else {
// Only condition1 true
}
} else {
// condition1 false
}

Example: Driving Eligibility​

if (age >= 18) {
if (hasLicense == 'y') {
printf("You can drive!\n");
} else {
printf("You need a license.\n");
}
} else {
printf("You must be 18+.\n");
}

πŸ”Έ The else-if Ladder​

Perfect for checking multiple conditions.

if (marks >= 90) grade = 'A';
else if (marks >= 80) grade = 'B';
else if (marks >= 70) grade = 'C';
else grade = 'F';

Example: Grade Calculator​

if (marks < 0 || marks > 100) {
printf("Invalid!\n");
} else if (marks >= 90) {
printf("Grade A\n");
} else if (marks >= 80) {
printf("Grade B\n");
} else {
printf("Grade C or below\n");
}

πŸ”Ή The switch-case Statement​

Efficient multi-way branching.

switch (expression) {
case value1: // do something
break;
case value2:
break;
default:
// fallback
}

Rules​

  • Expression must be int/char
  • Case values = constants only
  • break prevents fall-through
  • default is optional but useful

Example: Calculator​

switch(op) {
case '+': result = a + b; break;
case '-': result = a - b; break;
case '*': result = a * b; break;
case '/':
if (b != 0) result = a / b;
else printf("Division by 0!\n");
break;
default: printf("Invalid operator\n");
}

βš–οΈ if-else vs switch​

Featureif-elseswitch
Condition typeAnyint/char only
Multiple checksβœ… Flexible❌ Only equality
Floating pointβœ… Yes❌ No
Range checkingβœ… Easy❌ Hard
PerformanceSlower for many checksFaster for many cases
Best forComplex conditionsMenu-driven apps

πŸ§ͺ Practical Mini Projects​

Leap Year Checker​

if (year % 400 == 0) leap = 1;
else if (year % 100 == 0) leap = 0;
else if (year % 4 == 0) leap = 1;
else leap = 0;

Triangle Validator​

Checks if sides form a triangle and its type (Equilateral, Isosceles, Scalene).

Student Grade System​

Use if-else ladder for grades + switch for remarks. Perfect combo!


πŸŽ“ Quick Quiz​

Which statement is faster for checking 10 fixed integer values?

  • if-else ladder
  • switch-case βœ…
  • Nested if
  • None of the above

πŸ‘ That’s it for Unit 3!
Now your code can decide, branch, and react like a pro.
Next stop: Unit 4: Loops πŸ”„